<%@ Page language="vb" CodeFile="addInvoiceSeparate.aspx.vb" Inherits="addInvoiceSeparate_aspx_vb" %>

<%
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        vehicleID = Request.QueryString("vehicleID")
        If Len(vehicleID) > 0 Then 
            vehicleID = CLng(vehicleID)
        Else
            vehicleID = ""
        End IF
        If Len(vehicleID) > 0 Then 
            dbOpen()
            RecSet = Conn.Execute("Select v.regnr, v.vinNumber, p.partnerID, p.partnerName, c.countryID, c.country, c.countryCurrency, c.decimals From ((tblVehicle v INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = p.countryID) Where v.vehicleID = " & vehicleID & "")
            If Not RecSet.EOF Then 
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryID = RecSet.Fields("countryID").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        Else
            dbOpen()
            RecSet = Conn.Execute("Select partnerID, partnerName, countryID From tblPartner Order by partnerName")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    partnerID = RecSet.Fields("partnerID").Value
                    partnerName = RecSet.Fields("partnerName").Value
                    countryID = RecSet.Fields("countryID").Value
                    strSelectSubJS = strSelectSubJS + "new Array(" & partnerID & "," & countryID & ",""" & partnerName & """),"
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            dbOpen()
            RecSet = Conn.Execute("Select countryID From tblCountry Order by countryID")
            Do Until RecSet.EOF
                countryID = RecSet.Fields("countryID").Value
                strSelectSubJS = strSelectSubJS + "new Array(0," & countryID & ","" ------------------------------------  ""),"
                RecSet.MoveNext()
            Loop
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            strSelectSubJS = Left(strSelectSubJS, Len(strSelectSubJS) - 1)
            strSelectSubHeadline = "Select lease taker"
            selectSub()
        End IF
        Over()
        strJS = ""
        If Len(vehicleID) = 0 Then 
            strJS = strJS + "[""countryID"", ""<1""],"
            strJS = strJS + "[""partnerID"", ""<1""],"
        End IF
        strJS = strJS + "[""invoiceAmount"", ""==''""],"
        strJS = strJS + "[""uw"", ""==''""],"
        strJS = strJS + "[""invoiceDateYear"", ""<1""],"
        strJS = strJS + "[""invoiceDateMonth"", ""<1""],"
        strJS = strJS + "[""invoiceDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CREATE SEPARATE INVOICE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='addInvoiceSeparate.aspx?do=save' method='post' name='store_productsSearch' onsubmit=""javascript:return checkFields();"">")
        If Len(vehicleID) > 0 Then 
            Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
            Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='country' id='country' value='" & country & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
            Response.Write("<tr><td class='formBold'>Licence number:</td><td align='right'><input type='text' name='regnr' id='regnr' value='" & regnr & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
            Response.Write("<tr><td class='formBold'>VIN-number:</td><td align='right'><input type='text' name='vinNumber' id='vinNumber' value='" & vinNumber & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
            Response.Write("<input type='hidden' name='vehicleID' id='vehicleID' value='" & vehicleID & "'>")
            Response.Write("<input type='hidden' name='countryID' id='countryID' value='" & countryID & "'>")
            Response.Write("<input type='hidden' name='partnerID' id='partnerID' value='" & partnerID & "'>")
        Else
            Response.Write("<tr>")
            Response.Write("<td class='formBold'>Country:</td>")
            Response.Write("<td align='right'>")
            Response.Write("<select name='countryID' id='countryID' onchange=""set_child_listbox(this, document.store_productsSearch.partnerID,Product,'Products');"">")
            Response.Write("<option value=''> - Select country - ")
            dbOpen()
            RecSet = Conn.Execute("Select countryID, country From tblCountry")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    countryID = RecSet.Fields("countryID").Value
                    country = RecSet.Fields("country").Value
                    Response.Write("<option value='" & countryID & "'> " & country & " ")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Write("<option value=''> -------------------------------------- ")
            Response.Write("</select>")
            Response.Write("</td>")
            Response.Write("<td><div id='error_countryID' class='edit_error'>&laquo; OBS!<br></div></td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td class='formBold'>Partner:</td>")
            Response.Write("<td align='right'>")
            Response.Write("<select name='partnerID' id='partnerID' disabled>")
            Response.Write("<option value=''> - Select partner - ")
            Response.Write("<option value=''> -------------------------------------- ")
            Response.Write("</select>")
            Response.Write("</td>")
            Response.Write("<td><div id='error_partnerID' class='edit_error'>&laquo; OBS!<br></div></td>")
            Response.Write("</tr>")
        End IF
        Response.Write("<tr><td class='formBold'>Invoice Amount (" & countryCurrency & "):</td><td align='right'><input type='text' name='invoiceAmount' id='invoiceAmount' value='' size='40' maxlength='50' onkeyup='re=/([\d\,\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_invoiceAmount' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW (" & countryCurrency & "):</td><td align='right'><input type='text' name='uw' id='uw' value='0' size='40' maxlength='50' onkeyup='re=/([\d\,\-]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_uw' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Payment term (days):</td><td align='right'><input type='text' name='paymentTerm' id='paymentTerm' value='10' size='40' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='invoiceDateYear' id='invoiceDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateMonth' id='invoiceDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='invoiceDateDay' id='invoiceDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_invoiceDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_invoiceDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Invoice text:&nbsp; </td></tr>")
        Response.Write("<tr><td colspan='2' align='right'><textarea name='invoiceText' id='invoiceText' style='width: 100%; height: 50px'></textarea></td></tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Create invoice'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        vehicleID = Request.Form("vehicleID")
        partnerID = Request.Form("partnerID")
        countryID = Request.Form("countryID")
        invoiceAmount = Request.Form("invoiceAmount")
        uw = Request.Form("uw")
        paymentTerm = Request.Form("paymentTerm")
        invoiceDateYear = Request.Form("invoiceDateYear")
        invoiceDateMonth = Request.Form("invoiceDateMonth")
        invoiceDateDay = Request.Form("invoiceDateDay")
        invoiceText = Request.Form("invoiceText")
        partnerID = CLng(partnerID)
        countryID = CLng(countryID)
        invoiceAmount = CDbl(invoiceAmount)
        uw = CDbl(uw)
        paymentTerm = CLng(paymentTerm)
        invoiceDateYear = CLng(invoiceDateYear)
        invoiceDateMonth = CLng(invoiceDateMonth)
        invoiceDateDay = CLng(invoiceDateDay)
        If CLng(invoiceDateMonth) < 10 Then 
            invoiceDateMonth = "0" & invoiceDateMonth & ""
        End IF
        If CLng(invoiceDateDay) < 10 Then 
            invoiceDateDay = "0" & invoiceDateDay & ""
        End IF
        invoiceDate = invoiceDateYear & "-" & invoiceDateMonth & "-" & invoiceDateDay
        expiryDate = DateAdd("d", CDbl(paymentTerm), Convert.ToDateTime(invoiceDate))
        invoiceAmount = Replace(CStr(invoiceAmount), ",", ".")
        invoiceClaim = invoiceAmount
        If Len(invoiceText) > 0 Then 
            invoiceText = Trim(invoiceText)
            invoiceText = Replace(invoiceText, """", "&quot;")
            invoiceText = Replace(invoiceText, "'", "&#39;")
            invoiceText = Replace(invoiceText, vbCrLf, "<br>")
        End IF
        Response.Write("vehicleID = " & vehicleID & "<br>")
        Response.Write("countryID = " & countryID & "<br>")
        Response.Write("partnerID = " & partnerID & "<br>")
        Response.Write("invoiceAmount = " & invoiceAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("uw = " & uw & "<br>")
        Response.Write("invoiceDate = " & invoiceDate & "<br>")
        Response.Write("expiryDate = " & expiryDate & "<br>")
        Response.Write("paymentTerm = " & paymentTerm & "<br>")
        Response.Write("invoiceText = " & invoiceText & "<br>")
        dbOpen()
        Response.Write("Insert Into tblInvoiceSeparate (countryID, partnerID, userID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & countryID & "," & partnerID & "," & fragusLeasingUserID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')<br>")
        Conn.Execute("Insert Into tblInvoiceSeparate (countryID, partnerID, userID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & countryID & "," & partnerID & "," & fragusLeasingUserID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')")
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("SELECT MAX(invoiceSeparateID) AS invoiceID FROM tblInvoiceSeparate")
        invoiceID = RecSet.Fields("invoiceID").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Len(vehicleID) > 0 Then 
            dbOpen()
            Response.Write("Update tblInvoiceSeparate Set vehicleID = " & vehicleID & " Where invoiceSeparateID = " & invoiceID & "<br>")
            Conn.Execute("Update tblInvoiceSeparate Set vehicleID = " & vehicleID & " Where invoiceSeparateID = " & invoiceID & "")
            dbClose()
        End IF
        Response.Redirect("addInvoiceSeparate.aspx?subDo=%20-%20INVOICE%20CREATED")
    End IF
%>
